
import {userLogin} from '../api/login'
import router from '../router/index'
export default{
    namespaced:true,
    state:{
        userInfo:null
    },
    mutations: {
        initInfoMut(state, info) {
            state.userInfo = info;  //缓存用户信息,
            localStorage.setItem('userInfo',JSON.stringify(info)) //持久缓存
            if(!info){
                localStorage.clear()
                return
            }
            router.push('/')
        }
    },
    actions: {  //异步action,让状态机拥有请求异步接口的能力
        userLoginAct(context, obj) {
            userLogin(obj).then(res=>{
                context.commit('initInfoMut',res.data.data)
                if(res.data.code==100){
                    router.push('/reg')
                }
            })
        }
    }
}